<?php

class AccountTrackingController extends AdminController
{
  public function init()
  {
    Yii::app()->user->loginUrl = '/admin/site/login';
  }

  public function filters()
  {
    return array('accessControl',);
  }

  public function accessRules()
  {
    return array(
      array('allow',
            'actions' => array('view', 'list', 'create', 'update', 'delete'),
            'roles' => array('member'),),
      array('deny','users' => array('*'),),
    );
  }

  public function actionList()
  {
    $condition = array();

    if (!Constants::isAdmin()) {
      $condition['memberId'] = Yii::app()->user->id;
    }

    $accountTrackingDao = new AccountTrackingDAO();
    $trackingList = array();
    $total = $accountTrackingDao->countByCondition($condition);

    if (intval($total) > 0) {
      $currentPage = Yii::app()->request->getParam("page", 1);
      $from = ($currentPage - 1) * Constants::RECORD_OF_PAGE;
      $totalPage = ceil($total/Constants::RECORD_OF_PAGE);
      $trackingList = $accountTrackingDao->findByCondition($condition, $from);
    }

    $this->render('list',
      array(
        'trackingList' => $trackingList,
        'total' => $total,
        'totalPage' => $totalPage,
        'currentPage' => $currentPage,
      ));
  }

  public function loadModel($id)
  {
    $model=AccountTracking::model()->findByPk($id);
    if($model===null)
      throw new CHttpException(404,'The requested page does not exist.');
    return $model;
  }

  protected function performAjaxValidation($model)
  {
    if(isset($_POST['ajax']) && $_POST['ajax']==='account-tracking-form')
    {
      echo CActiveForm::validate($model);
      Yii::app()->end();
    }
  }
}
